package com.zzptc.sky.bdsjws.utils;


import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.ContactsContract;
import com.zzptc.sky.bdsjws.MyApplication;
import com.zzptc.sky.bdsjws.vo.Contact;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/4/23.
 */

public class MyUtil {
    public static SharedPreferences getSharedPre(){
        return MyApplication.getContext().getSharedPreferences("loginnew", Context.MODE_PRIVATE);
    }
    //判断SD卡是否存在
    public static boolean isSD(){
        boolean flag=Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if(flag){
            return true;
        }
        return false;
    }

    //得到储存路径
    public static File getDirs(){
        if(isSD()){
            return Environment.getExternalStorageDirectory();
        }
        //return Environment.getDownloadCacheDirectory();
        return Environment.getExternalStorageDirectory();
    }
    public static Intent getInstall(File file){
        Intent intent=new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
        return intent;
    }
    //求日期差
    public static int daysBetween(Date smdate,Date bdate) throws ParseException
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        smdate=sdf.parse(sdf.format(smdate));
        bdate=sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }
    public static int getVersionCode(Context context){
        try{
            PackageInfo packageInfo=context.getPackageManager().getPackageInfo(context.getPackageName(),0);
            return packageInfo.versionCode;
        }catch (PackageManager.NameNotFoundException e){
            e.printStackTrace();
            return 0;
        }
    }
    //复制文件到手机
    public static void copyFileToMobile(Context context,String dbname)throws Exception{
        URL url=context.getClass().getClassLoader().getResource("assets/"+dbname);
        //文件路径
        String dbfile="/data/data/"+MyApplication.getContext().getPackageName()+"/databases";
        File dbfileMobile=new File(dbfile);
        if(!dbfileMobile.exists()){
            dbfileMobile.mkdir();
        }
        File dbfilemobiles=new File(dbfileMobile+"/"+dbname);
        if(!dbfilemobiles.exists()){
            dbfilemobiles.createNewFile();
        }
        FileUtils.copyURLToFile(url,dbfileMobile);
        //第一个参数  是包含路径源文件   第二个参数  目的地包含路径的文件
    }
    public static List<Context> getAllContacts(){
        List<Context> lists=new ArrayList<Context>();
        ContentResolver contentResolver=MyApplication.getContext().getContentResolver();
        Cursor cursor=contentResolver.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
       if(cursor!=null){
            while (cursor.moveToNext()){
//            //获取ID
               int id=cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//            //获取联系人的姓名
               String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
               int num=cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
               String phonenum=null;
               //如果大于0，表示有电话号码
               if(num>0) {
                   phonenum = getPhonenumByID(id);
               }
//            //手机号码  关联到DATA表查询
//            //手机归属地  涉及外部数据库
//                   Contact contact = new Contact(id, name, phonenum, "");
//                   lists.add(contact);
               }
           }
        return lists;
    }
    public static String getPhonenumByID(int id){
        String telnum=null;
        ContentResolver contentResolver=MyApplication.getContext().getContentResolver();
        Cursor cursor=contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
                ContactsContract.Contacts._ID+"=?",new String[]{id+""},null);
        if(cursor!=null){
            //我这只取一个电话号码
            if(cursor.moveToNext()){
                telnum=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            }
        }
        return telnum;
    }
//    public void test() throws IOException {
//        File file=new File("c:/ccc/c");
//        if(!file.exists()){
//            file.mkdir();
//        }
//        File dbfile=new File("c:/cc/c/1.txt");
//        if(!dbfile.exists()){
//            dbfile.createNewFile();
//        }
//    }  mkdir是创建文件夹
}
